#include <qapplication.h>
#include "window.moc"
#include "glwidget.h"

#include "ucdinputfile.h"
#include "visStreamtube.h"

using namespace visualization;

class glW: public GLWidget
{
public:
	glW(QWidget *parent = 0) 
	     : GLWidget(parent)
	{}
	~glW()
  {
	   delete results;
	}
private:
	Model t;
	Point *sources;
	double xmin, xmax, ymin, ymax, zmin, zmax;
	vector<visualization::Point> *results;

public:
	GLuint makeObject()
	{
		GLuint list = glGenLists(1);
		glNewList(list, GL_COMPILE);

		UCDInputFile file(filename);
		cout << filename << endl;
		file.FGetModel(t);	
		t.buildKDTree();
		t.getBound(xmin, xmax, ymin, ymax, zmin, zmax);
		double c[3] =
		{
			(xmax + xmin)/2,
			(ymax + ymin)/2,
			(zmax + zmin)/2
		};
		cout << "Input the scale..." << endl;
		double scale;
		cin >> scale;
		glBegin(GL_LINES);
		drawFramework(xmin-c[0], xmax-c[0], ymin-c[1], ymax-c[1], zmin-c[2], zmax-c[2], scale);
		glEnd();

		visStreamTube s(&t);
		s.setIntegrationForward();
		s.makeSourcesFormCircle();

		Point center(0, 0, 17);
		double radius = 0.8;
		s.setTubeSources(&center, 1, &radius, 1, 10);

		s.setParameters(0.1, 200);	
		s.constructStreamSurface();
		results = s.getTheStreamSurface();

		glColor3f((GLfloat)0.1, (GLfloat)1, (GLfloat)0.4);
		glBegin(GL_TRIANGLES);
			for(size_t i = 0; i < results->size();)
			{
				glVertex3f( ((*results)[i].getX() - c[0])/scale, 
										((*results)[i].getY() - c[1])/scale, 
										((*results)[i].getZ() - c[2])/scale
									);
				i++;
				glVertex3f( ((*results)[i].getX() - c[0])/scale, 
										((*results)[i].getY() - c[1])/scale, 
										((*results)[i].getZ() - c[2])/scale);
				i++;
				glVertex3f( ((*results)[i].getX() - c[0])/scale, 
										((*results)[i].getY() - c[1])/scale, 
										((*results)[i].getZ() - c[2])/scale);
				i++;
			}
		glEnd();

		glEndList();
		return list;
	}
};
class win : public Window
{
public:
	win(glW *glwidget)
		: Window(glwidget)
	{}
	~win(){}
};

int main(int argc, char *argv[])
{
	if(argc<2) 
	{
		cout << "Input the data file please..." << endl;
		exit(-1);
	}
  QApplication app(argc, argv);
	glW *glw = new glW();
	glw->setFileName(argv[1]);
  win window(glw);
	window.generateWindow();
  window.show();
  app.exec();
	return 0;
}
